﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace mvvmCommon
{

    public static class PageAnimation
    {
        //SwivelToBackStoryboard
        private static Storyboard storyBoard;// = Application.Current.Resources["FadeInOut"] as Storyboard;
        private static Storyboard storyBoardBack;// = Application.Current.Resources["SwivelToBackStoryboard"] as Storyboard;
        private static Storyboard storyBoardFront;// = Application.Current.Resources["SwivelToFrontStoryboard"] as Storyboard;
        private static Grid front;
        public static Grid back;
        private static int style = 0;

        /// <summary>
        /// Load resouce style
        /// </summary>
        public static void LoadResource()
        {
            try
            {
                ResourceDictionary resource = new ResourceDictionary();
                resource.Source = new Uri("/mvvmCommon;component/style.xaml", UriKind.RelativeOrAbsolute);
                storyBoard = resource["FadeInOut"] as Storyboard;
                storyBoardBack = resource["SwivelToBackStoryboard"] as Storyboard;
                storyBoardFront = resource["SwivelToFrontStoryboard"] as Storyboard;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static void ToBack()
        {
            switch (style)
            {
                case 0:
                    storyBoard.Stop();
                    SetAnimation(front, back);
                    storyBoard.Begin();
                    break;
                case 1:
                    storyBoardFront.Stop();
                    storyBoardBack.Stop();
                    SetStyle_1(front, back);
                    storyBoardBack.Begin();
                    break;
            }
        }

        public static void ToFront()
        {
            switch (style)
            {
                case 0:
                    storyBoard.Stop();
                    SetAnimation(back, front);
                    storyBoard.Begin();
                    break;
                case 1:
                    storyBoardBack.Stop();
                    storyBoardFront.Stop();
                    //SetStyle_1(front, back);
                    storyBoardFront.Begin();
                    break;
            }

        }

        /// <summary>
        /// Using default animation style
        /// </summary>
        /// <param name="_front">front grid</param>
        /// <param name="_back">back grid</param>
        public static void SetObject(Grid _front, Grid _back)
        {
            style = 0;//default
            front = _front;
            back = _back;
        }

        private static void SetAnimation(Grid _front, Grid _back)
        {
            Storyboard.SetTarget(storyBoard.Children[0], _front);
            Storyboard.SetTarget(storyBoard.Children[1], _front);
            Storyboard.SetTarget(storyBoard.Children[2], _back);
            Storyboard.SetTarget(storyBoard.Children[3], _back);
        }

        /// <summary>
        /// Options animation style
        /// </summary>
        /// <param name="_front"> front grid</param>
        /// <param name="_back"> back grid</param>
        /// <param name="_style"> 0 - fade in out, 1 - swiveltoback-front</param>
        public static void SetObject(Grid _front, Grid _back, int _style)
        {
            switch (_style)
            {
                case 1:
                    _back.Projection = new PlaneProjection();
                    _front.Projection = new PlaneProjection();
                    _back.Visibility = Visibility.Collapsed;
                    break;
                default:
                    break;
            }
            style = _style;
            front = _front;
            back = _back;
        }

        private static void SetStyle_1(Grid _front, Grid _back)
        {

            Storyboard.SetTarget(storyBoardBack.Children[0], _back);
            Storyboard.SetTarget(storyBoardBack.Children[1], _front);
            Storyboard.SetTarget(storyBoardBack.Children[2], _front);
            Storyboard.SetTarget(storyBoardBack.Children[3], _back);

            Storyboard.SetTarget(storyBoardFront.Children[0], _back);
            Storyboard.SetTarget(storyBoardFront.Children[1], _front);
            Storyboard.SetTarget(storyBoardFront.Children[2], _front);
            Storyboard.SetTarget(storyBoardFront.Children[3], _back);
        }
    }
}
